using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IUPUI.StellarResults.Client
{
	/// <summary>
	/// A form to allow geocoding of addresses.
	/// </summary>
	public partial class GeoCoderForm : Form
	{
		private ININ.GIS.Address m_Address = new ININ.GIS.Address();
		private ININ.GIS.Coordinates m_Coordinates = null;

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="GeoCoderForm"/> class.
		/// </summary>
		public GeoCoderForm()
		{
			InitializeComponent();

			m_AddressPropertyGrid.SelectedObject = m_Address;
		}
		#endregion Constructors

		/// <summary>
		/// Handles the Click event of the m_GeoCodeButton control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void m_GeoCodeButton_Click(object sender, EventArgs e)
		{
			m_Coordinates = null;

			m_Coordinates = ININ.GIS.GISRepository.Provider.GeoCoder.GetCoordinates(
				m_Address);

			if (m_Coordinates != null)
			{
				m_LatitudeTextBox.Text = m_Coordinates.Latitude.ToString();
				m_LongitudeTextBox.Text = m_Coordinates.Longitude.ToString();
			}
			else
			{
				// Display error message if we could not geocode the address
				MessageBox.Show(
					this,
					Properties.Resources.FailedToGeoCode,
					Properties.Resources.ErrorTitle,
					MessageBoxButtons.OK,
					MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// Handles the LinkClicked event of the m_CopyToClipboardLinkLabel control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Forms.LinkLabelLinkClickedEventArgs"/> instance containing the event data.</param>
		private void m_CopyToClipboardLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (m_Coordinates != null)
			{
				Clipboard.SetData(
					System.Windows.Forms.DataFormats.Text, m_Coordinates.ToString());
			}
			else
			{
				Clipboard.SetData(
					System.Windows.Forms.DataFormats.Text, "");
			}
		}
	}
}